package com.example.dishfo.myapplication.customview.image;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 可以动态的显示图片
 * 而且对图片进行裁剪
 * 点击图片可以将图片显示出来
 */

public class ScalebleImageViews extends LinearLayout{
    public static final int SHOW_SINGLE=0x14;
    public static final int SHOW_MUTIL=0x15;
    public static final int SHOW_NONE=0X13;

    private int maxImageNum=3;
    private int showMode=SHOW_SINGLE;
    private int itemCount=1;
    private float WH= (float) (4.0/7f);
    //dp
    private int itemMargin=10;
    //内容填充相关的接口
    private LoadImageAdapter loadImageAdapter;
    private onItemClickListener listener;

    private int itemHeight;
    private int itemWidth;


    public ScalebleImageViews(Context context) {
        this(context,null);
    }

    public ScalebleImageViews(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScalebleImageViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public ScalebleImageViews(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        //设置基本的属性
        listener=new DefaultOnItemListener();
        loadImageAdapter =new DefaultLoadImageAdapter();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setOrientation(HORIZONTAL);
        LinearLayout.LayoutParams params= (LayoutParams) getLayoutParams();
        params.width=LayoutParams.MATCH_PARENT;
        params.leftMargin=params.rightMargin=itemMargin;
        setLayoutParams(params);

        if(loadImageAdapter.getImageCount()==0){
            MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        }else if(loadImageAdapter.getImageCount()==1){
            heightMeasureSpec=
                    MeasureSpec.makeMeasureSpec((int) (MeasureSpec.getSize(widthMeasureSpec)*WH)
                            ,MeasureSpec.UNSPECIFIED);
        }else {
            heightMeasureSpec=
                    MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec)/maxImageNum
                            ,MeasureSpec.UNSPECIFIED);
        }

        if(showMode==SHOW_NONE){

        }else if(showMode==SHOW_SINGLE){
            itemHeight=MeasureSpec.getSize(heightMeasureSpec)-2*itemMargin;
            itemWidth=MeasureSpec.getSize(widthMeasureSpec);
        }else if(showMode==SHOW_MUTIL){
            itemWidth=(MeasureSpec.getSize(widthMeasureSpec)-2*itemMargin)/maxImageNum;
            itemHeight=MeasureSpec.getSize(heightMeasureSpec)-2*itemMargin;
        }

        for (int i=0;i<itemCount;i++){
            @SuppressLint("DrawAllocation")
            View imageView=getChildAt(i);
            @SuppressLint("DrawAllocation")
            LinearLayout.LayoutParams childparams=new LinearLayout.LayoutParams(itemWidth,itemHeight);
            childparams.rightMargin=itemMargin;
            imageView.setLayoutParams(childparams);
        }


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //在此处进行imageView的布置
        super.onLayout(changed, l, t, r, b);
    }

    public int getShowMode() {
        return showMode;
    }

    public void setShowMode(int showMode) {
        this.showMode = showMode;
    }

    @FunctionalInterface
    public interface onItemClickListener{
        void onItemClick(ImageView view, int index);
    }

    public interface LoadImageAdapter {
        int getImageCount();

        Bitmap getBitMap(int index,ImageView view);
    }

    public void setListener(onItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 设置加载的adpter
     * @param loadImageAdapter
     */
    public void setLoadImageAdapter(LoadImageAdapter loadImageAdapter) {
        this.loadImageAdapter = loadImageAdapter;
        this.itemCount= loadImageAdapter.getImageCount();
        if(itemCount>3){
            throw new RuntimeException("the image count must be less than 3");
        }

        if(itemCount==1){
            showMode=SHOW_SINGLE;
        }else {
            showMode=SHOW_MUTIL;
        }

        buildContent();
        invalidate();
    }

    /**
     * 加载图片需要手动调用
     */
    public void loadImage(){
        for (int i=0;i<itemCount;i++){
            loadImageAdapter.getBitMap(i,getImageView(i));
        }
    }

    private void buildContent() {
        for (int i=0;i<itemCount;i++){
            @SuppressLint("DrawAllocation")
            ImageView imageView=new ImageView(getContext());
            addView(imageView);
            final int index=i;
            imageView.setClickable(true);
            imageView.setOnClickListener(v -> this.listener.onItemClick((ImageView) v,index));
        }
    }

    public ImageView getImageView(int index){
        return (ImageView) getChildAt(index);
    }


    class DefaultLoadImageAdapter implements LoadImageAdapter {

        @Override
        public int getImageCount() {
            return 0;
        }

        @Override
        public Bitmap getBitMap(int index,ImageView imageView) {
            return null;
        }
    }


    class DefaultOnItemListener implements onItemClickListener{

        @Override
        public void onItemClick(ImageView view, int index) {

        }
    }

}
